#include "declare.hpp"

// 遍历查找
std::vector<Node>::iterator MapSystem::_SearchWithOrder(INT32U linkId)
{
    _log(INFO, "正在查找 Link ID 为 %d 的记录", linkId);
    for (auto it = _vector_array.begin(); it != _vector_array.end(); ++it)
    {
        if (it->_ulLinkID == linkId)
        {
            std::cout << "Record Size: " << it->_usRecSize
                      << ", Link ID: " << it->_ulLinkID
                      << ", Name Size: " << it->_usRoadNameSize
                      << ", Start Node Info: " << it->_ulStaNodeInfo
                      << ", Road Name: " << it->_pcRoadName << std::endl;
            return it;
        }
    }
    _log(WARNING, "没有找到 Link ID 为 %d 的记录", linkId);
    return _vector_array.end();
}

// 二分查找
std::vector<Node>::iterator MapSystem::_SearchWithBinary(INT32U linkID)
{
    this->_QuickSort_Implement(0, _vector_array.size() - 1);

    size_t left = 0;
    size_t right = _vector_array.size() - 1;

    _log(INFO, "正在进行二分查找 Link ID 为 %d 的记录", linkID);

    while (left <= right)
    {
        size_t mid = left + (right - left) / 2;
        if (_vector_array[mid]._ulLinkID == linkID)
        {
            std::cout << "找到记录: "
                      << "Record Size: " << _vector_array[mid]._usRecSize
                      << ", Link ID: " << _vector_array[mid]._ulLinkID
                      << ", Name Size: " << _vector_array[mid]._usRoadNameSize
                      << ", Start Node Info: " << _vector_array[mid]._ulStaNodeInfo
                      << ", Road Name: " << _vector_array[mid]._pcRoadName << std::endl;
            return _vector_array.begin() + mid;
        }
        else if (_vector_array[mid]._ulLinkID < linkID)
        {
            left = mid + 1;
        }
        else
        {
            right = mid - 1;
        }
    }
    _log(WARNING, "没有找到 Link ID 为 %d 的记录", linkID);
    return _vector_array.end();
}

// 哈希查找
std::unordered_map<INT32U, Node>::iterator MapSystem::_SearchWithHash(INT32U linkID)
{
    std::unordered_map<INT32U, Node> hashTable;
    for (const auto &node : _nodeList)
    {
        hashTable[node._ulLinkID] = node;
    }

    _log(INFO, "正在进行哈希查找 Link ID 为 %d 的记录", linkID);

    auto it = hashTable.find(linkID);
    if (it != hashTable.end())
    {
        std::cout << "找到记录: "
                  << "Record Size: " << it->second._usRecSize
                  << ", Link ID: " << it->second._ulLinkID
                  << ", Name Size: " << it->second._usRoadNameSize
                  << ", Start Node Info: " << it->second._ulStaNodeInfo
                  << ", Road Name: " << it->second._pcRoadName << std::endl;
        return it;
    }
    else
    {
        _log(WARNING, "没有找到 Link ID 为 %d 的记录", linkID);
    }
    return hashTable.end();
}

// 二叉排序树查找
std::vector<Node>::iterator MapSystem::_SearchWithTree(INT32U linkID)
{
    BinarySearchTree tree;

    for (auto &node : _vector_array)
    {
        tree.Insert(node);
    }

    _log(INFO, "正在进行树查找 Link ID 为 %d 的记录", linkID);

    Node *foundNode = tree.Search(linkID);
    if (foundNode)
    {
        std::cout << "找到记录: "
                  << "Record Size: " << foundNode->_usRecSize
                  << ", Link ID: " << foundNode->_ulLinkID
                  << ", Name Size: " << foundNode->_usRoadNameSize
                  << ", Start Node Info: " << foundNode->_ulStaNodeInfo
                  << ", Road Name: " << foundNode->_pcRoadName << std::endl;
        auto it = std::find_if(_vector_array.begin(), _vector_array.end(),
                               [&](const Node &n) { return n._ulLinkID == linkID; });
        return it;
    }
    else
    {
        _log(WARNING, "没有找到 Link ID 为 %d 的记录", linkID);
    }
    return _vector_array.end();
}
